<template>
   <div>
       <el-breadcrumb separator-class="el-icon-arrow-right">
           <el-breadcrumb-item :to="{ path: '/' }">
               <i class="el-icon-s-promotion"></i>
               后台管理</el-breadcrumb-item>
           <el-breadcrumb-item :to="{ path: '/sys-admin/permission/admin'}">
               权限管理</el-breadcrumb-item>
           <el-breadcrumb-item>
               添加权限</el-breadcrumb-item>
       </el-breadcrumb>
       <el-divider></el-divider>
       <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
           <el-form-item label="权限名" prop="name">
               <el-input v-model="ruleForm.name"></el-input>
           </el-form-item>
           <el-form-item label="权限范围" prop="value">
               <el-input v-model="ruleForm.value"></el-input>
           </el-form-item>
           <el-form-item label="简介" prop="description">
               <el-input v-model="ruleForm.description"></el-input>
           </el-form-item>
           <el-form-item label="序号" prop="sort">
               <el-input v-model="ruleForm.phone"></el-input>
           </el-form-item>
           <el-form-item label="角色" prop="roleIds">
               <el-select v-model="ruleForm.roleIds" multiple placeholder="请选择"
             d>
                   <el-option
                           v-for="item in roleListOptions"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id">
                   </el-option>
               </el-select>
           </el-form-item>

           <el-form-item>
               <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
               <el-button @click="resetForm('ruleForm')">重置</el-button>
               <el-button type="warning" @click="$router.push('/sys-admin/permission/permission')">返回列表</el-button>
           </el-form-item>
       </el-form>
   </div>
</template>

<script>
    export default {
        data() {
            return {
                roleListOptions:[],
                ruleForm: {
                    name:'测试权限名',
                    value:'xx/xx/xx',
                    description: '测试权限001的简介',
                    sort:'',
                    roleIds: []
                },
                rules: {
                  roleIds: [
                    {required: true, message: '请选择角色', trigger: 'blur'}
                  ]
                },
            };

        },
        methods:{
            loadRoleList() {
                let url ='http://localhost:9281/role/list';
                console.log('url = ' + url);

                let localJwt = localStorage.getItem('localJwt');

                this.axios
                    .create({
                        'headers': {
                            'Authorization': localJwt
                        }
                    })
                    .get(url).then((response) => {
                    let jsonResult = response.data;
                    console.log(jsonResult)
                    this.roleListOptions = jsonResult.data;
                    console.log(this.roleListOptions)
                });
            },

            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url ='http://localhost:9281/permission/add-new';
                        console.log('url = ' + url);
                        let formData =this.qs.stringify(this.ruleForm);
                        let localJwt = localStorage.getItem('localJwt');
                        this.axios
                            .create({
                                'headers': {
                                    'Authorization': localJwt
                                }
                            })
                            .post(url,formData).then((response)=>{
                            let jsonResult =response.data;
                            if(jsonResult.state==20000){
                                this.$message({
                                    message: '添加权限成功！',
                                    type: 'success'
                                });
                                this.resetForm(formName);
                            }else {
                                this.$alert(jsonResult.message, '错误', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                    }
                                });
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
        },
        
        mounted() {
            this.loadRoleList();
        },
    }

</script>

<style scoped>

</style>